export default function(G6) {
    G6.registerBehavior('elementSelected', {
        getDefaultCfg() {
            return {
                // 配置项：是否允许多选，默认为 false
                multiple: false,
                responseTypes: ['node'], // 选中: 响应的元素, 默认为节点
            };
        },
        getEvents() {
            return {
                'node:click': 'onClick',
                'edge:click': 'onClick',
                'canvas:click': 'onClick',
                contextmenu: 'onContextmenu',
            };
        },
        onClick(e) {
            if (!this.multiple) this._clearSelected();
            // 如果是画布则清空所有选中
            if (!e.item) {
                this._clearSelected();
                this._elementClick(null);
                return false;
            }
            // 暂时只处理 node 和 edge 的点击
            const type = e.item.getType();

            if (!this.responseTypes.includes(type)) {
                return false;
            }
            // if (type !== 'node' && type !== 'edge') {
            //     this._clearSelected();
            //     this._elementClick(null);
            //     return false;
            // }
            e.item.toFront();
            this.graph.setItemState(e.item, 'selected', true);

            const selectedItems = this._getSelectedItems();
            const model = e.item.getModel();
            if (this.multiple) {
                if (!selectedItems[`${type}s`]?.find((i) => i.id === model.id)) {
                    selectedItems[`${type}s`].push(model);
                }
            } else {
                selectedItems[`${type}s`] = [model];
            }

            this._setSelectedItems(selectedItems);
            this._elementClick(model, type);
        },

        onContextmenu(e) {
            e.preventDefault();
            e.stopPropagation();

            // 如果是画布，不作任何操作
            if (!e.item) {
                return false;
            }
            // 实例上没有回调函数不予处理
            if (!this.graph.onContextmenu && typeof this.graph.onContextmenu !== 'function') return false;

            const model = e.item.getModel();
            const type = e.item.getType();

            // 不包含的类型不响应,暂只对右键处理
            if (!this.responseTypes.includes(type)) {
                return false;
            }
            // 暂时未作多选操作
            this._clearSelected();

            this.graph.setItemState(e.item, 'selected', true);
            const selectedItems = this.graph.get('selectedItems');
            if (this.multiple) {
                if (!selectedItems[`${type}s`]?.find((i) => i.id === model.id)) {
                    selectedItems[`${type}s`].push(model);
                }
            } else {
                selectedItems[`${type}s`] = [model];
            }
            this._setSelectedItems(selectedItems);
            this._elementClick(model, type);
            const options = {
                coord: {
                    x: e.canvasX,
                    y: e.canvasY,
                },
                model,
                type,
            };
            this._onContextmenu(options);
        },
        /**
         * 清除所有 节点和线 的选中状态
         */
        _clearSelected() {
            const nodesSelected = this.graph.findAllByState('node', 'selected');
            const edgesSelected = this.graph.findAllByState('edge', 'selected');
            nodesSelected.forEach((node) => {
                this.graph.setItemState(node, 'selected', false);
                // this.graph.clearItemStates(node, 'selected');
            });
            edgesSelected.forEach((edge) => {
                this.graph.setItemState(edge, 'selected', false);
            });

            this.graph.set('selectedItems', { nodes: [], edges: [] });
        },
        _getSelectedItems() {
            return this.graph.get('selectedItems') || { nodes: [], edges: [] };
        },
        _setSelectedItems(selectedItem) {
            this.graph.set('selectedItems', selectedItem);
        },
        /**
         *
         * @param {Object} itemModel 元素的 model
         * @param {String} type 节点 node 、 线 edge
         */
        _elementClick(itemModel, type) {
            this.graph.elementClick && this.graph.elementClick(itemModel, type);
        },
        /**
         * @param {return} options coord model type
         */
        _onContextmenu(options) {
            this.graph.onContextmenu && this.graph.onContextmenu(options);
        },
    });
}
